home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / NCSA⁄BYU TCP⁄IP / mactools.c.original < prev    next >
Text File  |  1991-06-27  |  11KB  |  438 lines

  1. /*
  2. *  mactools.c                by Gaige B. Paulsen
  3. *
  4. *  adapted from pctools.c      by Tim Krauskopf
  5. ****************************************************************************
  6. *                                                                          *
  7. *      part of:                                                            *
  8. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  9. *      by Tim Krauskopf                                                    *
  10. *                                                                          *
  11. *      National Center for Supercomputing Applications                     *
  12. *      152 Computing Applications Building                                 *
  13. *      605 E. Springfield Ave.                                             *
  14. *      Champaign, IL  61820                                                *
  15. *                                                                          *
  16. *                                                                          *
  17. ****************************************************************************
  18. *
  19. *  those generic tool-type things that only work on Macs.
  20. *  includes all hardware-level calls to Ethernet that are unique to the Mac
  21. *
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <String.h>
  26.  
  27. #include <Dialogs.h>
  28. #include <Files.h>
  29. #include <AppleTalk.h>
  30. #include <Events.h>
  31. #include <Memory.h>
  32.  
  33. #include "protocol.h"
  34. #include "data.h"
  35. #include "fastnet.h"
  36. #include "configrec.h"
  37. #include "ethertalk.h"
  38. #include "atalk.h"                /* Dlayer to atalk conversion */
  39. #include "dlayer.h"
  40. #include "ip.h"
  41. #include "ether.h"
  42. #include "elistener.h"
  43. #include "listener.h"
  44. #include "maclook.h"
  45.  
  46. #ifdef MPW
  47. #define    movmem(y,x,z)    memcpy(x,y,z)
  48. #endif
  49.  
  50. /*
  51.  * THE ALL-POWERFUL VARIABLE !!!!!!!!!!
  52.  */
  53.  
  54. int EtherNet
  55.   /*
  56.     specifies hardware interface to use.
  57.     Values are
  58.         -99        -- no hardware, using MacTCP (set up in userd.c)
  59.         -2        -- FastNet
  60.         -1        -- Kinetics Ether SC or Ether SE
  61.         0        -- LocalTalk ("built-in")
  62.         1        -- 3Com EtherLink/NB (or Apple EtherTalk)
  63.         9 .. 14 -- EtherLink/NB in specified NuBus slot
  64.   */
  65.     = 0;        /* default to LocalTalk */
  66.  
  67.  
  68.  
  69. /**********************************************************************/
  70. /*  netarpme
  71. *   send an arp to my address.  arpinterpret will notice any response.
  72. *   Checks for adapters which receive their own broadcast packets.
  73. */
  74. int netarpme
  75.   (
  76.     char *s
  77.   )
  78.   {
  79.     if (EtherNet) {    
  80.         reqarp(s);
  81.     }
  82.     return(0);
  83.   }
  84.  
  85. /*
  86.  * ATdlayersend (ptr, size)
  87.  *  send size from ptr out the AT port
  88.  *    All outgoing conversions done here ! 
  89.  */
  90. ATdlayersend(ptr,size)            /*MAC: replaced */
  91. ARPKT *ptr;
  92. unsigned size;
  93. {
  94.     char *todata;
  95.     AddrBlock *destptr;
  96.     int ptype, tsize;
  97.     char wds[14], header[18];
  98.     MPPParamBlock pb;
  99.     static ATARPKT arpbuffer;
  100.  
  101.     int err;
  102.  
  103.     ptype = AIP;                                    /* Default to sending an IP Packet */
  104.  
  105.     pb.DDPsocket=IPSock;                            /* Sorry.... */
  106.     pb.DDPchecksumFlag=FALSE;                        /* who knows why, but we don't */
  107.     pb.DDPwdsPointer=wds;                            /* Here we go..... */
  108.     pb.MPPcsCode=246;                                /* We want to send a packet */
  109.  
  110.     todata=(char *)ptr + sizeof(DLAYER);            /* Point at the real data... */
  111.     (uint8 *) destptr = &ptr->d.dest[0];            /* Where is the destination ? */
  112.     tsize = size- sizeof(DLAYER);                    /* Get transmission size */
  113.  
  114.     if (!memcmp( destptr, bseed, 6))                    /* Watch for Broadcasts */
  115.         movmem(ATbseed, destptr, 4);
  116.  
  117.     if (ptr->d.type == EARP) {
  118.         ptype=AARP;                                        /* Arp packet */
  119.         arpbuffer.hrd = 3;                                /* ATalk */
  120.         arpbuffer.hln = 4;
  121.         arpbuffer.pln = 4;
  122.         arpbuffer.pro = ptr->pro;
  123.         arpbuffer.op  = ptr->op;
  124.         movmem( &ptr->sha[0], &arpbuffer.sha[0], 4);    /* Move addresses */
  125.         movmem( &ptr->spa[0], &arpbuffer.spa[0], 4);
  126.         movmem( &ptr->tha[0], &arpbuffer.tha[0], 4);
  127.         movmem( &ptr->tpa[0], &arpbuffer.tpa[0], 4);
  128.         todata= (char *) &arpbuffer;
  129.         todata+= sizeof(ATdlayer);                        /* New address to go from */
  130.         tsize = sizeof(ATARPKT)- sizeof(ATdlayer);
  131.         }
  132.         
  133.   /* note incorrect definition of BuildDDPwds in AppleTalk.h:
  134.     the destAddress arg is an AddrBlock, not a pointer to one! */
  135.     BuildDDPwds( wds, header, todata, *(AddrBlock **) destptr, ptype, tsize );
  136.  
  137.     err=PWriteDDP( &pb, FALSE);
  138.     if (err!=0) putln("DDPW Error = %d");
  139. #ifdef SENDNOTICE    
  140.     else putln("send");
  141. #endif
  142.     return(err);
  143.     
  144. }
  145.  
  146. /*****************************************************************
  147.  *     E  T  H  E  R  N  E  T   /   E  T  H  E  R  T  A  L  K    *
  148.  *****************************************************************/
  149.  
  150. struct ETHERWDSstruct {
  151.     short len1;
  152.     DLAYER *ptr1;
  153.     short len2;
  154.     DLAYER *ptr2;
  155.     short term;
  156.     } EWDS;
  157.  
  158. int ETdlayersend            /*MAC: replaced */
  159.   (
  160.     DLAYER *ptr,
  161.     unsigned size
  162.   )
  163. {
  164.     EtParam ET;
  165.     int err;
  166.  
  167.     err = 0;
  168.     EWDS.len1=size;                    /* Could it would it in a WDS? */
  169.     EWDS.ptr1=ptr;
  170.     EWDS.len2=0;
  171.  
  172.     ET.address = &EWDS;
  173.  
  174.     /* err = */ ETcall( &ET, EWrite);
  175.     if (/* err != */ 0)
  176.         putln("ETW Error = %d");
  177. #ifdef SENDNOTICE    
  178.     else putln("send");
  179. #endif
  180.     return(err);
  181.     
  182. }
  183.  
  184. /**********************************************************************/
  185. /*   ETdemux    - Ethernet Version of DEMUX routine
  186. *
  187. *  returns the number of packets demuxed
  188. */
  189.  
  190. int ETdemux
  191.   (
  192.     int all
  193.   )
  194.     {
  195.     unsigned getcode;
  196.     int nmuxed,sizered;
  197.     typedef struct peek {
  198.         unsigned short count;
  199.         unsigned char dest[DADDLEN],
  200.             me[DADDLEN];
  201.         unsigned short type;
  202.     } Dpeek;
  203.  
  204.     Dpeek *firstlook;
  205.  
  206.     nmuxed = 0;
  207.  
  208.     do {                                    /* while all flag is on */
  209.  
  210.         if (bufinfo.bufbig > 0) { /*MAC*/
  211.  
  212.             nmuxed++;
  213.             firstlook = (Dpeek *)(bufinfo.bufread);     /*MAC where packet is */
  214.  
  215.             getcode = firstlook->type;            /* where does it belong? */
  216.  
  217.             switch (getcode) {                    /* what to do with it? */
  218.                 case EARP:
  219.                 case ERARP:
  220.                     arpinterpret((ARPKT *) firstlook->dest); /* handle [R]ARP packet */
  221.                     break;
  222.                 case EIP: 
  223.                     ipinterpret((IPKT *) firstlook->dest);    /* handle IP packet */
  224.                     break;
  225.                 default:
  226.                     break;
  227.             }
  228.  
  229.             sizered=(firstlook->count+sizeof(Dpeek));
  230.             bufinfo.bufbig-=sizered;
  231.             
  232. #ifdef DEBUGONLY
  233.             {
  234.             char s[60];
  235.             sprintf(s,"buf: %d, pkt: %d",bufinfo.bufbig,sizered);
  236.             putln(s);
  237.             }
  238. #endif
  239.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  240.  
  241.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  242.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  243.             
  244.         }
  245.         else {
  246.  
  247.             if (ETdeafreset()) {
  248.                 /* clear deaf flag, buffer overrun */
  249.                 putln("warning: buffer deaf, restarting");
  250.                 }
  251.  
  252.             all = 0;
  253.             }
  254.  
  255.  
  256.     } while (all);            /* should we look for more to deal with? */
  257.  
  258.  
  259.     return(nmuxed);          /* no packets anymore */
  260.  
  261. }
  262.  
  263. /************************************************************************/
  264. /*  dlayersend
  265. *
  266. *  usage:   err = dlayersend(ptr,size)
  267. *      err = 0 for successful, non-zero error code otherwise
  268. *      ptr is to a dlayer packet header
  269. *      size is the number of bytes total
  270. *
  271. *  This particular dlayer routine is for Ethernet.  It will have to be
  272. *  replaced for any other dlayer.
  273. *
  274. *  Assumes that dlayer-dependent initializations were made in protinit.c
  275. *  to help efficiency.
  276. *
  277. *  Ethernet addresses are resolved at higher levels because they will only
  278. *  need to be resolved once per logical connection, instead of once per
  279. *  packet.  Not too layer-like, but hopefully modular.
  280. *
  281. */
  282.  
  283. int dlayersend
  284.   (
  285.     DLAYER *ptr,
  286.     int size
  287.   )
  288. {
  289.     if (EtherNet== 0)
  290.         return( ATdlayersend(ptr,size));
  291.     if (EtherNet==-2)
  292.         return( FNdlayersend(ptr,size));
  293.     else
  294.         return( ETdlayersend(ptr,size));
  295. }
  296.  
  297. int ATdemux
  298.   (
  299.     int all
  300.   )
  301. {
  302.     unsigned getcode;
  303.     int nmuxed,sizered, count;
  304.     ATARPKT *firstlook;
  305.     static ARPKT arpbuffer;
  306.     nmuxed = 0;
  307.  
  308.     do {                                    /* while all flag is on */
  309.  
  310.         if (bufinfo.bufbig > 0) {
  311.             nmuxed++;
  312.             firstlook = (ATARPKT *)(bufinfo.bufread);     /*MAC where packet is */
  313.  
  314.             getcode = firstlook->d.type;            /* where does it belong? */
  315.             count = firstlook->d.count;
  316.             movmem( firstlook->d.dest, arpbuffer.d.dest,4);
  317.             movmem( firstlook->d.me,   arpbuffer.d.me  ,4);
  318.             
  319.             switch (getcode) {                    /* what to do with it? */
  320.                 case AARP:
  321.                     arpbuffer.d.type=EARP;                            /* Arp packet */
  322.                     arpbuffer.hrd = 1;                                /* Enet */
  323.                     arpbuffer.hln = 6;
  324.                     arpbuffer.pln = 4;
  325.                     arpbuffer.pro = firstlook->pro;
  326.                     arpbuffer.op  = firstlook->op;
  327.                     movmem( &firstlook->sha[0], &arpbuffer.sha[0], 4);        /* Move addresses */
  328.                     movmem( &firstlook->spa[0], &arpbuffer.spa[0], 4);
  329.                     movmem( &firstlook->tha[0], &arpbuffer.tha[0], 4);
  330.                     movmem( &firstlook->tpa[0], &arpbuffer.tpa[0], 4);
  331.                     arpinterpret(&arpbuffer);    /* handle ARP packet */
  332.                     break;
  333.                 case AIP:
  334.                     arpbuffer.d.type=EIP;
  335.                     movmem( &arpbuffer, firstlook, sizeof(DLAYER) );
  336.                     ipinterpret((IPKT *) firstlook);
  337.                     break;
  338.                 default:
  339.                     break;
  340.             }
  341.  
  342.             sizered = count + sizeof(DLAYER);
  343.             bufinfo.bufbig-=sizered;
  344.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  345.  
  346.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  347.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  348.  
  349.         }
  350.         else 
  351.             all = 0;
  352.  
  353.  
  354.     } while (all);            /* should we look for more to deal with? */
  355.  
  356.  
  357.     return(nmuxed);          /* no packets anymore */
  358.  
  359. }
  360.  
  361. /**********************************************************************/
  362. /*   demux
  363. *      find the packets in the buffer, determine their lowest level
  364. *  packet type and call the correct interpretation routines
  365. *
  366. *  the 'all' parameter tells demux whether it should attempt to empty
  367. *  the input packet buffer or return after the first packet is dealt with.
  368. *
  369. *  returns the number of packets demuxed
  370. */
  371.  
  372. int demux
  373.   (
  374.     int all
  375.   )
  376. {
  377.     if (EtherNet==0)
  378.         return( ATdemux(all));
  379.     if (EtherNet==-2)
  380.         return( FNdemux(all));
  381.     else
  382.         return( ETdemux(all));
  383.  
  384. }
  385.  
  386. initbuffer()
  387.     {
  388.     (Ptr) raw=NewPtr(17000);        /* Added by GBP */
  389.     if (raw==NULL) return(-1);
  390.     bufinfo.bufpt = bufinfo.bufread = bufinfo.buforg
  391.                                          = raw;    /*  start at the beginning */
  392.  
  393.     bufinfo.bufend = raw+14500;            /* leave 2K breathing room, required */
  394.     bufinfo.buflim = 12000;             /* another 2K breathing room */
  395.     bufinfo.bufbig = 0;                    /* Added by GBP */
  396.  
  397.     if (EtherNet==0)
  398.         InitBufPtr(&bufinfo);
  399.     else if (EtherNet != -2) 
  400.         ETInitBufPtr( &bufinfo);
  401.     return(0);
  402. }
  403.  
  404. /***************************************************************************/
  405. /* dlayerinit
  406. *  Do machine dependent initializations of whatever hardware we have
  407. *  (happens to be 3com ethernet board here ) 
  408. */
  409. int dlayerinit
  410.   (
  411.     void
  412.   )
  413. {
  414.  
  415.     if (initbuffer())
  416.         return(-1);
  417.     if (EtherNet==0) 
  418.         return(atopen());
  419.     if (EtherNet==-2)
  420.         return(FNopen());
  421.     else
  422.         return(ETopen());    /* Pass the Type along */
  423. }
  424.  
  425. int dlayershut
  426.   (
  427.     void
  428.   )
  429. {
  430.     if (EtherNet==0)
  431.         return(atclose());
  432.     if (EtherNet==-2)
  433.         return(FNclose());
  434.     else
  435.         return(ETclose());
  436. }
  437.  
  438.